package com.liveneo.adapter.mobile.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.google.common.collect.Maps;
import com.liveneo.adapter.base.entity.PrpmCaseInfo;
import com.liveneo.adapter.base.entity.PrpmOrg;
import com.liveneo.adapter.base.entity.PrpmSurveyUser;
import com.liveneo.adapter.base.entity.PrpmVideoConnection;
import com.liveneo.adapter.base.entity.PrpmVideoUser;
import com.liveneo.adapter.base.entity.PrpmVideoUserExample;
import com.liveneo.adapter.base.entity.ZteResultRecord;
import com.liveneo.adapter.base.entity.ZteResultRecords;
import com.liveneo.adapter.base.mapper.PrpmSurveyUserMapper;
import com.liveneo.adapter.base.mapper.PrpmVideoConnectionMapper;
import com.liveneo.adapter.base.mapper.PrpmVideoUserMapper;
import com.liveneo.adapter.base.service.InvokedZteClientService;
import com.liveneo.adapter.base.support.BaseServiceImpl;
import com.liveneo.adapter.base.support.LogUtils;
import com.liveneo.adapter.mobile.service.CaseInfoService;
import com.liveneo.adapter.mobile.service.MobileVideoService;
import com.liveneo.adapter.webservice.client.zte.video.QueryBySql;
import com.liveneo.adapter.webservice.client.zte.video.QueryBySqlResponse;
import com.liveneo.system.base.utils.Constant;
import com.liveneo.system.base.utils.XmlUtils;
import com.liveneo.system.service.OrgService;

/** 视频服务实现类
 * @author wangxx
 *
 */
@Service
public class MobileVideoServiceImpl extends BaseServiceImpl implements MobileVideoService {
    @Autowired
    private PrpmSurveyUserMapper           surveyUserMapper;
    @Autowired
    private OrgService              orgService;
    @Autowired
    private InvokedZteClientService invokedZteClientService;
    @Autowired
    private PrpmVideoConnectionMapper      videoConnectionMapper;
    @Autowired
    private PrpmVideoUserMapper            videoUserMapper;
    @Autowired
    private CaseInfoService caseInfoService;

    @Override
    public Map<String, Object> getQueueDataByUserCode(String surveyUserCode,String caseNo,String source) throws Exception {
        PrpmSurveyUser user = surveyUserMapper.findByUserCode(surveyUserCode);
        String orgId = null;
        if(!source.equals("C")){
    	   if (user == null) {
               return null;
           }
    	   orgId = user.getOrgId();
        }
        
        if(source.equals("C")){
        	//C端根据案件号查询组织机构
        	PrpmCaseInfo prpmCaseInfo = caseInfoService.getCaseInfo(caseNo);// 根据uuid查询案件信息
        	orgId = prpmCaseInfo.getOrgId();
        }
        
        try {
            PrpmOrg org = orgService.queryById(orgId);
            String vcid = org.getOrgVcid();
            String skillId = org.getOrgSkill();
            // 调用中兴接口
            LogUtils.debug("传入中兴接口的数据：vcid:" + vcid + ", skillId:" + skillId);
            QueryBySql request = new QueryBySql();
            request.setVcid(vcid);
            request.setSqlid("ebaseproc.mp_queuecntandforecast");
            request.setVariables("<variables><variable1>" + vcid + "</variable1><variable2>" + skillId + "</variable2></variables>");
            QueryBySqlResponse response = invokedZteClientService.queryBySqlRequest(request);
            String resultInfo = response.getResultinfo();
            LogUtils.debug("中兴接口反馈原始数据：" + resultInfo);
            // 解析接口返回数据
            if (resultInfo != null && !"".equals(resultInfo)) {
                ZteResultRecords records = XmlUtils.toZTEObject(resultInfo);
                if (records != null && records.getRecord() != null) {
                    ZteResultRecord record = records.getRecord().get(0);
                    String forecast = record.getForecast();
                    String queuecnt = record.getQueuecnt();
                    LogUtils.debug("中兴接口反馈数据：forecast：" + forecast + ", queuecnt:" + queuecnt);
                    Map<String, Object> videoMap = Maps.newHashMap();
                    videoMap.put("forecast", forecast);
                    videoMap.put("queuecnt", queuecnt);
                    return videoMap;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        return null;
    }

    @Override
    public void addVideoConnection(String surveyUserCode, String terminalID) {
        PrpmVideoConnection videoConnection = videoConnectionMapper.selectByPhoneNo(surveyUserCode);
        PrpmSurveyUser user = surveyUserMapper.findByUserCode(surveyUserCode);
        if (videoConnection != null) {
            LogUtils.debug("用户与视频控件ID关系已存在，更新数据");
            // 如果数据存在，更新数据表
            videoConnection.setTerminalId(terminalID);
            videoConnection.setCreateTime(new Date());
            videoConnection.setModifyBy(user.getSurveyName());
            videoConnectionMapper.updateByPrimaryKey(videoConnection);
        } else {
            // 如果不存在，增加数据
            LogUtils.debug("用户与视频控件ID关系不存在，增加数据");
            videoConnection = new PrpmVideoConnection();
            videoConnection.setVideoConnectionId(Constant.MYSQL_UUID);
            videoConnection.setMobile(user.getMobile());
            videoConnection.setTerminalId(terminalID);
            videoConnection.setCreateBy(user.getSurveyName());
            videoConnection.setCreateTime(new Date());
            videoConnection.setUserCode(user.getSurveyUserCode());
            videoConnectionMapper.insert(videoConnection);
        }
    }

    @Override
    public PrpmVideoUser getVideoUserByUserCode(String userCode) {
    	 PrpmVideoUserExample example = new PrpmVideoUserExample();
         example.createCriteria().andUserCodeEqualTo(userCode);
         List<PrpmVideoUser> list = videoUserMapper.selectByExample(example);
         return list.size() > 0 ? list.get(0) : null;
    }
}
